// packageC/o2o/o2oSearchMultiple/o2oSearchMultiple.js
var location = require("../../../mybehaviors/location/location");
var app = getApp();
Page({
  behaviors: [location],
  /**
   * 页面的初始数据
   */
  data: {
    city: '',
    point: {
      lat: '',
      lng: ''
    },
    stores: [],
    inputs: "",

    //more
    page: 1, //分页数，当前页数
    isLoadMore: true, //判断是否要加载更多的标志
    total_page: 0, //总页数
    listData: [],
    networkLoading: false,

    screenMenu: [{
      name: '商品',
      api: 'goods.goods.search-goods'
    }],
    screenType: ["good"],
    screenIndex: 0,
    screenShow: false,

    goods_template: {
      name: '02'
    },
    openTemplate: 0 //门店列表样式
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    await this.getScreenList(options.search_type);
    if (options.city && options.city !== '1') {
      if (options.city) {
        this.setData({
          city: options.city
        });
      }
      if (options.point) {
        this.setData({
          point: JSON.parse(options.point)
        });
      }
      this.getListData();
    } else {
      this._getLocation(() => {
        this.getListData();
      });
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.isLoadMore) {
      console.log("111111111111111");
      this.getMoreData();
    } else {
      console.log('没有更多数据');
    }
  },

  setScreenShow() {
    this.setData({
      screenShow: !this.data.screenShow
    });
  },
  setScreenIndex(evt) {

    let index = evt.currentTarget.dataset.index;
    if (index == this.data.screenIndex) return;
    this.setData({
      screenIndex: index,
      screenShow: false
    });
    this.getListData();
  },

  gotoGoods(evt) {
    let id = evt.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + id
    });
  },

  gotoSupplier(evt) {
    let id = evt.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/packageA/member/supplier/SupplierShop/SupplierShop?id=' + id,
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let point = JSON.stringify(this.data.point);
    return {
      title: '',
      path: '/packageC/o2o/o2oSearchMultiple/o2oSearchMultiple?city=' + this.data.city + '&point=' + point,
    };
  },
  initPage() {
    this.setData({
      listData: [],
      networkLoading: false
    });
    this.data.page = 1;
    this.data.total_page = 0;
    this.data.isLoadMore = true;

  },
  getListData() {
    this.initPage();
    let urlStr = app.getNetAddresss(this.data.screenMenu[this.data.screenIndex].api);
    let point = app.qqMapTransBMap(parseFloat(this.data.point.lng), parseFloat(this.data.point.lat));
    urlStr += '&lng=' + point.lng;
    urlStr += '&lat=' + point.lat;
    urlStr += '&city_name=' + this.data.city;
    urlStr += '&page=' + this.data.page;
    let json = {};
    if (this.data.screenIndex == 0) {
      json["search[keyword]"] = this.data.inputs;
    } else if (this.data.screenMenu[this.data.screenIndex].name == '门店商品') {
      json["goods_title"] = this.data.inputs;
    } else {
      json.kwd = this.data.inputs;
    }
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        this.setData({
          networkLoading: true
        });
        if (res.result != 1) return app.tips(res.msg);
        this.setData({
          isLoadMore: true,
          total_page: res.data.last_page,
          listData: res.data.data,
        });
        if (!this.data.total_page) {
          this.data.total_page = 0;
        }

        if (res.data.hasOwnProperty("store_list_style")) {
          this.setData({
            openTemplate: res.data.store_list_style || 0
          });
        }

      }
    });
  },
  //获取更多数据
  getMoreData() {
    let urlStr = app.getNetAddresss(this.data.screenMenu[this.data.screenIndex].api);
    let point = app.qqMapTransBMap(parseFloat(this.data.point.lng), parseFloat(this.data.point.lat));
    urlStr += '&lng=' + point.lng;
    urlStr += '&lat=' + point.lat;
    urlStr += '&city_name=' + this.data.city;

    // 防止多次请求分页数据
    this.data.isLoadMore = false;
    if (this.data.page >= this.data.total_page) {
      console.log("111111111111111", this.data.page, this.data.total_page);
      return;
    } else {
      this.data.page++;
      let json = {
        page: this.data.page
      };
      if (this.data.screenIndex == 0) {
        json["search[keyword]"] = this.data.inputs;
      } else if (this.data.screenMenu[this.data.screenIndex].name == '门店商品') {
        json["goods_title"] = this.data.inputs;
      } else {
        json.kwd = this.data.inputs;
      }
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          var res = resdata.data;

          if (res.result == 1) {
            this.data.isLoadMore = true;
            var myData = res.data.data;
            this.setData({
              listData: this.data.listData.concat(myData)
            });
          } else {
            this.data.isLoadMore = false;
            this.data.page--;
          }
        }
      });
    }
  },


  getScreenList(search_type = "good") {
    return new Promise((resolve, reject) => {
      let urlStr = app.getNetAddresss('plugins.get-enabled-plugins');
      app._getNetWork({
        url: urlStr,
        success: resdata => {
          let res = resdata.data;

          let screenMenu = this.data.screenMenu;
          let screenType = this.data.screenType;

          if (res.data.is_supplier == 1) {
            screenMenu.push({
              name: "供应商",
              api: "plugin.supplier.frontend.supplier.get-supplier-list"
            });
            screenType.push("supplier");
          }

          if (res.data.is_store == 1) {
            screenMenu.push({
              name: "门店",
              api: "plugin.store-cashier.frontend.store.store.get-store-list-to-page"
            });
            screenType.push("store");
            screenMenu.push({
              name: "门店商品",
              api: "plugin.store-cashier.frontend.store.store.get-store-goods-list"
            });
            screenType.push("storeGood");
          }

          if (res.data.is_hotel == 1) {
            screenMenu.push({
              name: "酒店",
              api: "plugin.hotel.frontend.hotel.goods.get-hotel-goods-by-title"
            });
            screenType.push("hotel");
          }

          this.setData({
            screenMenu,
            screenType
          });
          let index = screenType.indexOf(search_type);
          if (this.data.screenMenu[index]) {
            this.setData({
              screenIndex: index
            });
          }
          resolve();
        }
      });
    });
  },

});